home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / Kibitz⁄THINK C / IdleTasks.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-22  |  4.8 KB  |  182 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:        Kibitz
  5. ** File:        idletasks.c
  6. ** Written by:  Eric Soldan
  7. **
  8. ** Copyright © 1990-1991 Apple Computer, Inc.
  9. ** All rights reserved.
  10. */
  11.  
  12.  
  13.  
  14. /*****************************************************************************/
  15.  
  16.  
  17.  
  18. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  19. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  20. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  21.  
  22. #ifndef __UTILITIES__
  23. #include "Utilities.h"
  24. #endif
  25.  
  26.  
  27.  
  28. /*****************************************************************************/
  29.  
  30.  
  31.  
  32. #pragma segment Main
  33. void    DoIdleTasks(Boolean allowComputerMoves)
  34. {
  35.     WindowPtr        window, compMoveWindow;
  36.     unsigned long    compMoveTick;
  37.     FileRecHndl        game, compMoveGame;
  38.     FileRecPtr        frPtr;
  39.     short            twoPlayer, moveColor, myColor, update, syncClocks, i;
  40.     Boolean            compMovesWhite, compMovesBlack, sendSyncGame;
  41.  
  42.     static unsigned long    clockSyncTick;
  43.  
  44.     DynamicBalloonHelp();
  45.  
  46.     compMoveTick = -1;
  47.  
  48.     if (syncClocks = (clockSyncTick + 1800 < TickCount()))
  49.         clockSyncTick = TickCount();
  50.             /* Syncronize clocks every 30 seconds. */
  51.  
  52.  
  53.     for (window = FrontWindow();
  54.          window;
  55.          window = (WindowPtr)(((WindowPeek)window)->nextWindow)
  56.     ) {
  57.         if (IsAppWindow(window)) {
  58.  
  59.             game = (FileRecHndl)GetWRefCon(window);
  60.             twoPlayer = (*game)->doc.twoPlayer;
  61.  
  62.             if (update = UpdateTime(game, true)) DrawTime(game);
  63.  
  64.             if (update == 2) {
  65.                 if (twoPlayer) SendGame(game, kIsMove);
  66.                     /* Send it as a move, since we want the alert to
  67.                     ** show up for the opponent. */
  68.                 AlertIfGameOver(game);
  69.                 return;
  70.             }
  71.  
  72.             if ((*game)->doc.resync >= kResync) {
  73.                 sendSyncGame = true;
  74.                     /* We may need to sync up.  Assume we will. */
  75.  
  76.                 if ((*game)->doc.configColorChange) {
  77.                     if ((*game)->doc.myColor != (*game)->doc.configColor) {
  78.                         (*game)->doc.myColor = (*game)->doc.configColor;
  79.                         (*game)->doc.invertBoard ^= 1;
  80.                         SetPort(window);
  81.                         ImageDocument(game, true);
  82.                     }
  83.                     (*game)->doc.configColorChange = false;
  84.                     AdjustGameSlider(game);
  85.                     sendSyncGame = false;
  86.                 }
  87.  
  88.                 if ((*game)->doc.configTimeChange) {
  89.                     for (i = 0; i < 2; ++i)
  90.                         (*game)->doc.timeLeft[i] =
  91.                             (*game)->doc.displayTime[i] =
  92.                                 (*game)->doc.configTime[i];
  93.                     (*game)->doc.configTimeChange = false;
  94.                     UpdateTime(game, false);
  95.                     DrawTime(game);
  96.                     sendSyncGame = false;
  97.                 }
  98.  
  99.                 if (sendSyncGame) {
  100.                     if ((*game)->doc.gotUpdateTick + 120 < TickCount()) {
  101.                         /* Wait for 2 secs since last game update before syncing.
  102.                         ** This is so that we don't send a sync while the opponent
  103.                         ** is still clicking on the arrow.  Without this delay, the
  104.                         ** scrollbar of the opponent will jump around after he is
  105.                         ** done scrolling.  It would eventually end up correct,
  106.                         ** but it looks bad.  2 secs is enough time (generally) for
  107.                         ** the opponent to receive the sync from the last click, so
  108.                         ** the scroll won't jump around.  Also, 2 secs is probably
  109.                         ** more time than the user would take between clicks on
  110.                         ** the arrow. */
  111.  
  112.                         if ((*game)->doc.creator)        /* Only the creator can sync. */
  113.                             if (twoPlayer) SendGame(game, kHandResync);
  114.                                 /* Make sure the boards are in sync. */
  115.  
  116.                         (*game)->doc.resync = kIsMove;
  117.                             /* Back to life as usual. */
  118.  
  119.                         DoSetCursor(nil);
  120.                             /* Re-calc the cursor. */
  121.                     }
  122.                 }
  123.             }
  124.  
  125.             frPtr          = *game;
  126.             compMovesWhite = frPtr->doc.compMovesWhite;
  127.             compMovesBlack = frPtr->doc.compMovesBlack;
  128.             myColor        = frPtr->doc.myColor;
  129.             moveColor      = WhosMove(game);
  130.  
  131.             if (frPtr->doc.arrangeBoard)
  132.                 compMovesWhite = compMovesBlack = 0;
  133.  
  134.             if (twoPlayer) {
  135.                 if (myColor == moveColor) {
  136.                     if (syncClocks)
  137.                         SendMssg(game, kTimeMssg);
  138.                             /* Sync up the clocks every 30 seconds.  Only do
  139.                             ** this if it is our move, since the player who
  140.                             ** owns the move also owns the clock. */
  141.                 }
  142.                 else
  143.                     compMovesWhite = compMovesBlack = false;
  144.             }
  145.             if (
  146.                 ((compMovesWhite) && (moveColor == WHITE)) ||
  147.                 ((compMovesBlack) && (moveColor == BLACK))
  148.             ) {
  149.                 if (compMoveTick > frPtr->doc.compMoveTick) {
  150.                     if (GameStatus(game) == kGameContinues) {
  151.                         compMoveTick   = frPtr->doc.compMoveTick;
  152.                         compMoveWindow = window;
  153.                         compMoveGame   = game;
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.     }
  159.  
  160.     if (allowComputerMoves) {
  161.         if (compMoveTick != -1) {
  162.             if ((*compMoveGame)->doc.resync == kIsMove) {
  163.                 (*compMoveGame)->doc.compMoveTick = TickCount();
  164.                 if (ComputerMove(compMoveGame)) {
  165.                     SetPort(compMoveWindow);
  166.                     ImageDocument(compMoveGame, true);
  167.                     AdjustGameSlider(compMoveGame);
  168.                     twoPlayer = (*compMoveGame)->doc.twoPlayer;
  169.                     DrawButtonTitle(compMoveGame, twoPlayer);
  170.                     UpdateGameStatus(compMoveGame);
  171.                     if (twoPlayer) SendGame(compMoveGame, kIsMove);
  172.                     AlertIfGameOver(compMoveGame);
  173.                     DoSetCursor(nil);
  174.                 }
  175.             }
  176.         }
  177.     }
  178. }
  179.  
  180.  
  181.  
  182.